//
//  SettingsViewController.m
//  WGFGlobalHouse
//
//  Created by waiguofang on 15/4/15.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//  分销商 －－系统设置

#import "SettingsViewController.h"
#import "ReSetViewController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "BrokerDataManage.h"
#import "config.h"
#import "TxtReadVc.h"
#import "FeedbackVC.h"
#import "AboutUsVc.h"
#import "AppDelegate.h"
#import "FTWCache.h"
#import "UIView+circular.h"
#import "MyXMPP.h"
#import <StoreKit/StoreKit.h>
#import "SDImageCache.h"



@interface SettingsViewController ()<UIAlertViewDelegate,UIAlertViewDelegate,SKStoreProductViewControllerDelegate>

@property (strong, nonatomic) IBOutlet UIScrollView *mainView;
- (IBAction)clickBtn:(UIButton *)btn;
@property (weak, nonatomic) IBOutlet UIButton *pwdBtn;
@property (weak, nonatomic) IBOutlet UILabel *versionLab;
@property (weak, nonatomic) IBOutlet UIButton *outBtn;

@property (weak, nonatomic) IBOutlet UIView *hideShowView;
@end

@implementation SettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];

    
}


-(void)initView
{
    [self.outBtn viewToCircular];
    [self.titleView setMianTitle:@"系统设置"];
    self.titleView.doneBtn.hidden = YES;
    self.mainView.contentSize = CGSizeMake(UIScreenWidth,SCROLLVIWEHEIGHT);
    
    [self checkVersion];
}



-(void)checkVersion{
    
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |
    NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    NSInteger year = [dateComponent year];
    NSInteger month = [dateComponent month];
    
    if(year==2015&&(month==6||month==7)){
        [self.hideShowView setHidden:year];
    }else{
        [self.hideShowView setHidden:false];
    }
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *itunesURL = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APP_ID];
        ResulteMod *mod = [Connect doGetWithOutCaChe:itunesURL];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            [MBProgressHUD hideHUD];
            
            
            if (mod.resltState ==ResultNetError) {
                
                
            }else if(mod.resltState==ResultFail){
                
                
            }else{
                
                int versionCount = [mod.resultDic[@"resultCount"] intValue];
                
                if (versionCount>1) {
                    
                    [self.versionLab setText:[NSString stringWithFormat:@"有新版本可更新"]];
                }else{
                    
                    [self.versionLab setText:@"已经是最新版本"];
                }
            }
        });
    });

}



- (IBAction)clickBtn:(UIButton *)btn;
{
    NSInteger tag = btn.tag;
    switch (tag) {
        case 0: //密码设置
        {
            ReSetViewController *vc = [[ReSetViewController alloc]initWithNibName:@"ReSetViewController" bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
        }
        break;
        case 1: //用户反馈
            [self.navigationController pushViewController:[FeedbackVC new] animated:YES];
            
            break;
        case 2: //清除缓存
       
        {
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定要清除缓存吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alter.tag = 50;
            [alter show];
        }
            
            
            break;
        case 3: //退出账号
        {
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定退出当前账号？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alter.tag = 100;
            [alter show];
        }
          
            break;
        case 4://打分鼓励一下吧
        {
            
//            然后将 http:// 替换为 itms:// 或者 itms-apps://：
//            
//           itms://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8
//            itms-apps:// itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8
           
            NSString *str = @"itms-apps://itunes.apple.com/gb/app/yi-dong-cai-bian/id1012907804?mt=8";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            
        }
            break;
            
        default:
            break;
    }
}

-(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
        return;
    
    if (alertView.tag == 100) { //退出登录
       
        [[MyXMPP sharedMyXMPP] disconnect];
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@"NO" forKey:@"login"];
        [defaults synchronize];
        
        
        NSArray *array = self.navigationController.viewControllers;
        for (UIViewController *vc in array) {
            [vc removeFromParentViewController];
        }
        
        AppDelegate *delegar = [UIApplication sharedApplication].delegate;
        [delegar goToLogin];

    }else if (alertView.tag == 50){//清除缓存
       
        
        [[SDImageCache sharedImageCache] clearDisk];
            
            //    [[SDImageCache sharedImageCache] clearMemory];//可有可无
            
        

            float tmpSize = [[SDImageCache sharedImageCache] getSize];
        
        NSLog(@"[SDImageCache sharedImageCache]   %f",tmpSize);
            
//            NSString *clearCacheName = tmpSize >= 1 ? [NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"清理缓存(%.2fK)",tmpSize * 1024];
//            
//            [configDataArray replaceObjectAtIndex:2 withObject:clearCacheName];
//            
//            [configTableView reloadData];
        [FTWCache cleanAll];
    }
    
}

#pragma mark 关于我们
- (IBAction)aboutBtnOnClick:(id)sender {
    
    [self.navigationController pushViewController:[AboutUsVc new] animated:YES];
    
}

//消息推送
- (IBAction)tuiSongBtnOnClick:(id)sender {

}

@end
